Functions to Objects

TAD

Introduction

Perhaps one of the most frustrating things when reading about Object Orientated programming is that it is never clearly described in terms of 'normal' programming terms (like functions, data-structures, vectors, sub-routines etc..). I can remember reading two large books which threw around phrases like classes, methods, properties and interfaces but never really managed to describe what they meant.

Hopefully this article will convince you that Object-Orientated Programming is very similiar in many ways to 'normal' programming with its functions, procedures, sub-routines, data-structures, variables and code vectors.

The code mostly relates to Flash 5 and Flash MX actionscripting, but because it is very similiar to JavaScript and JScript it should still be useful.

What is an object?

The simplest way to think about an object is like a data-structure (with its properties) with some code functions (its methods) attached to it.

What is a class?

In order to create (instantiate) an object we must first define what it should look like, its variables (properties) and its functions (methods). This is the same as defining a MACRO or a data STRUCTure, but it is given the name of a class. Once we have defined a class, we can create new instances of that object (kinda like allocating a block of memory).

What is a constructor?

When an object is instantiated (created) it performs some initialisation operations (like setting variables to their default values, performing some calculations etc). All these operations are performed in the object's constructor code.

What are properties?

Simply put, a property is a data variable (or sub-object) assigned to an object. You can think of it like a member of a data-structure, or an element in an array with its own unique name and value. Just like members of a structure properties use the '.' dot notation, like this:

	object.propertyName

What are methods?

These are functions associated to a particular object. You can always spot a method by its ( and ) brackets (yes, just like normal functions).

	object.methodName()

What are sub-objects?

An object can have properties which are sub-objects (just like normal data structures, you can build large structures from smaller sub-structures). You access them in exactly the same way as you would for a nested data structure using the '.' dot notation.

	object.subobject.propertyName
	object.subobject.methodName()

and you can continue this:

	object.subobject.subobject.subobject.propertyName
	object.subobject.subobject.subobject.methodName()

What is inheritance?

This basically means, if a .property or .method( ) does not exist in the current object then look in its parent object. It allows both data and functions to be shared between many different classes of objects without having to explictly define each and every property and method again and again and again.

What is 'this'?

The 'this' keyword relates to the current object that we are working with. It basically means itself - its own properties and methods.

What is the 'Object' object?

<rant> This is perhaps one of the dumbest naming ideas in the history of the universe </rant> Every single object you create inherits a small set of common properties and methods from the Object object (note: the big 'O' in Object!). One of the most useful methods is the .toString( ) method. This attempts to return a string based on an object's primary value.

What is a prototype?

This is just like what its name means, it's a prototype (a blue-print) for an object's property or method. Its like making a default property/method for an object which can be replaced with a different property/method later on.

The inheritance nature of Object-Orientated languages makes use of the prototype chain in order to search for inherited properties and methods from parent objects or from grand-parent objects etc...

What is an Array?

Okay, this may sound like a silly question, but you may be surprised by the answer. As far as I know, arrays (in the normal sense) do not exist in Object-Orientated languages. Instead each data element is described just like a property (it has its own value and unique name). I guess this makes the dynamic handling of arrays much, much easier (and slower) because you can randomly create/access/delete any element in the array without having to resize the other array elements.

It means you can have an array with only two elements [0] and [1000000] without having to waste space storing all of the other non-existing elements inbetween 0 and 1000000.

As you can see, the array has two properties with names '0' and '1000000'.

What is the 'in' keyword?

One of the most useful features of objects is the ability to search through its own properties and methods. This is especially true for non-linear arrays (like the example above with only elements [0] and [1000000] defined). The 'in' keyword is used with a variation of the FOR loop in order to step through each, defined property/method. In the example above it would give us property names of '0' and '1000000' so we only have to deal with properties that exist (2 in our case).

What are the [ and ] characters?

These are usually used to refer to a particular element in an array, but they can also be used to access dynamically constructed property and method names. The example below will set properties 'count0, count1, count2.... count9' of the object 'myobject' to 0.

	for (var i=0; i<10; i++) {
		myobject["count"+i] = 0;
	}

Functions, Numbers, Strings and stuff

You will probably not surprised to hear that all of the normal data types are in fact objects. Each object has its own methods (the string object has the normal string slice, search, upperCase, lowerCase operations) and it also inherits the common properties/methods from the Object object.

What is a Getter &emp; Setter?

These are a cross between properties and methods (or more correctly read and write methods pretending to be property variables). Whenever you read from a property a 'Getter' function is invoked that returns the actual value. And likewise, when you write to a property a 'Setter' function is invoked which can perform some checks on the passed data before updating the actual property's value.

Using Getter and Setter functions you can control how properties are accessed, validate their values, perform calculations and even make them Read-Only or Write-Only if you wish.

Closing words

Well, I hope that has made the big, bad world of Object-Orientated programming seem a little less frightening. Next look at some very simple code..

Happy Readin'

TAD